/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RacaoSelector.java
 *
 * Created on Apr 19, 2009, 10:08:18 AM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.RacaoController;
import doghost.entities.Racao;

/**
 *
 * @author duo
 */
public class RacaoSelector extends javax.swing.JDialog {

    protected RacaoController racaoController;
    protected Racao selectedRacao;

    /** Creates new form RacaoSelector */
    public RacaoSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        racaoController = new RacaoController();
        racaoTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("racaoRecordSelected",
                                !isRacaoRecordSelected(), isRacaoRecordSelected());
                    }
                });
    }

    public Racao getSelectedRacao() {
        return selectedRacao;
    }

    public void setSelectedRacao(Racao selectedRacao) {
        this.selectedRacao = selectedRacao;
    }

    public boolean isRacaoRecordSelected() {
        return racaoTable.getSelectedRow() != -1;
    }

    @Action
    public void newRacao() {
        racaoController.newRacao(doghostPUEntityManager, racaoList);
    }

    @Action(enabledProperty = "racaoRecordSelected")
    public void editRacao() {
        int index = racaoTable.getSelectedRow();
        racaoController.setRacao(racaoList.get(racaoTable.
                convertRowIndexToModel(index)));
        racaoController.editRacao(doghostPUEntityManager, racaoList,
                racaoTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "racaoRecordSelected")
    public void deleteRacao() {
        int index = racaoTable.getSelectedRow();
        racaoController.setRacao(racaoList.get(racaoTable.
            convertRowIndexToModel(index)));
        racaoController.deleteRacao(doghostPUEntityManager, racaoList,
            racaoTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelRacaoSelector() {
        setSelectedRacao(null);
        setVisible(false);
    }

    @Action(enabledProperty = "racaoRecordSelected")
    public void selectRacao() {
        int index = racaoTable.getSelectedRow();
        setSelectedRacao(racaoList.get(racaoTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterRacao() {
        TableRowSorter racaoSorter = (TableRowSorter) racaoTable.getRowSorter();
        String text = racaoFilterTextField.getText();
        racaoFilterTextField.setText("");
        if(text.length()==0) {
            racaoSorter.setRowFilter(null);
        } else {
            racaoSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(RacaoSelector.class);
        racaoQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery(resourceMap.getString("racaoQuery.query")); // NOI18N
        racaoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(racaoQuery.getResultList());
        racaoFilterTextField = new javax.swing.JTextField();
        racaoFilterButton = new javax.swing.JButton();
        racaoSelectionPanel = new org.jdesktop.swingx.JXPanel();
        racaoScrollPane = new javax.swing.JScrollPane();
        racaoTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        selectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        racaoFilterTextField.setName("racaoFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(RacaoSelector.class, this);
        racaoFilterButton.setAction(actionMap.get("filterRacao")); // NOI18N
        racaoFilterButton.setName("racaoFilterButton"); // NOI18N

        racaoSelectionPanel.setName("racaoSelectionPanel"); // NOI18N

        racaoScrollPane.setName("racaoScrollPane"); // NOI18N

        racaoTable.setAutoCreateRowSorter(true);
        racaoTable.setName("racaoTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, racaoList, racaoTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        racaoScrollPane.setViewportView(racaoTable);
        racaoTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("racaoTable.columnModel.title0")); // NOI18N

        javax.swing.GroupLayout racaoSelectionPanelLayout = new javax.swing.GroupLayout(racaoSelectionPanel);
        racaoSelectionPanel.setLayout(racaoSelectionPanelLayout);
        racaoSelectionPanelLayout.setHorizontalGroup(
            racaoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(racaoSelectionPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(racaoScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        racaoSelectionPanelLayout.setVerticalGroup(
            racaoSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(racaoSelectionPanelLayout.createSequentialGroup()
                .addComponent(racaoScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        newButton.setAction(actionMap.get("newRacao")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editRacao")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteRacao")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelRacaoSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        selectButton.setAction(actionMap.get("selectRacao")); // NOI18N
        selectButton.setName("selectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(racaoFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(racaoFilterButton))
                    .addComponent(racaoSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addGap(33, 33, 33)
                        .addComponent(selectButton)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(racaoFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(racaoFilterButton))
                .addGap(18, 18, 18)
                .addComponent(racaoSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(newButton)
                            .addComponent(editButton)
                            .addComponent(deleteButton)
                            .addComponent(cancelButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(selectButton)))
                .addGap(52, 52, 52))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RacaoSelector dialog = new RacaoSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton racaoFilterButton;
    private javax.swing.JTextField racaoFilterTextField;
    private java.util.List<doghost.entities.Racao> racaoList;
    private javax.persistence.Query racaoQuery;
    private javax.swing.JScrollPane racaoScrollPane;
    private org.jdesktop.swingx.JXPanel racaoSelectionPanel;
    private javax.swing.JTable racaoTable;
    private javax.swing.JButton selectButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
